package com.ktg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.ktgcommon.domain.ProProcess;
import com.ktg.ktgcommon.domain.mainData.MdWorkshop;
import com.ktg.ktgcommon.domain.mainData.MdWorkstation;
import com.ktg.ktgcommon.filter.TokenVerifyFilter;
import com.ktg.ktgcommon.query.QueryWorkstation;
import com.ktg.mapper.ProcessMapper;
import com.ktg.mapper.WorkshopMapper;
import com.ktg.mapper.WorkstationMapper;
import com.ktg.service.WorkstationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class WorkstationServiceImpl implements WorkstationService {

    @Autowired
    private WorkstationMapper workstationMapper;

    @Autowired
    private WorkshopMapper workshopMapper;

    @Autowired
    private ProcessMapper processMapper;

    @Override
    public Page<MdWorkstation> selectPage(Page<MdWorkstation> page, QueryWorkstation queryWorkstation) {
        Page<MdWorkstation> workstationPage = workstationMapper.selectPage(page, queryWorkstation);
        return workstationPage;
    }

    @Override
    public MdWorkstation checkWorkstationById(Long workstationId) {
        MdWorkstation mdWorkstation = workstationMapper.checkWorkstationById(workstationId);
        return mdWorkstation;
    }

    @Override
    public Integer AddOrChWorkstation(MdWorkstation mdWorkstation) {
        //解析jwt获取user信息
        TokenVerifyFilter tokenVerifyFilter = new TokenVerifyFilter();
        if (mdWorkstation.getWorkstationId() != null) {
            //设置更新时间
            mdWorkstation.setUpdateTime(new Date());
            //设置更新者名称
            mdWorkstation.setUpdateBy(tokenVerifyFilter.getUserName().getUsername());
            MdWorkstation mdWorkstation1 = CheckWorkshopAndProcess(mdWorkstation);
            return workstationMapper.changeMdWorkstation(mdWorkstation1);
        } else {
            //设置创建时间
            mdWorkstation.setCreateTime(new Date());
            //设置创建者名称
            mdWorkstation.setCreateBy(tokenVerifyFilter.getUserName().getUsername());
            MdWorkstation mdWorkstation2 = CheckWorkshopAndProcess(mdWorkstation);
            return workstationMapper.addMdWorkstation(mdWorkstation2);
        }
    }

    @Override
    public Integer delWorkstation(Long workstationId) {
        Integer integer= workstationMapper.delWorkstation(workstationId);
        return integer;
    }

    @Override
    public Integer delWorkstationIdArray(List<Long> arrayWorkstationId) {
        Integer integer= workstationMapper.delWorkstationIdArray(arrayWorkstationId);
        return integer;
    }

    //查询工作站添加进入的配套信息
    //有车间表 工序表
    public MdWorkstation CheckWorkshopAndProcess(MdWorkstation mdWorkstation) {
        // 检查并设置车间信息
        if (mdWorkstation.getWorkshopId() != null) {
            MdWorkshop mdWorkshop = workshopMapper.checkWorkshopById(mdWorkstation.getWorkshopId());
            if (mdWorkshop != null) {
                mdWorkstation.setWorkshopCode(mdWorkshop.getWorkshopCode());
                mdWorkstation.setWorkshopName(mdWorkshop.getWorkshopName());
            }
        }
        // 检查并设置工序信息
        if (mdWorkstation.getProcessId() != null) {
            ProProcess proProcess = processMapper.checkProcessById(mdWorkstation.getProcessId());
            if (proProcess != null) {
                mdWorkstation.setProcessCode(proProcess.getProcessCode());
                mdWorkstation.setProcessName(proProcess.getProcessName());
            }
        }
        System.out.println("mdWorkstation:"+mdWorkstation);
        return mdWorkstation;
    }
}
